
package com.companymodule.dao;
import com.companymodule.bean.TbCompanyDepartMiddle;

import java.sql.*;
import java.io.*;
import java.util.*;
import util.*;
public class TbCompanyDepartMiddleDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbCompanyDepartMiddle tbCompanyDepartMiddle=null;
    private List<TbCompanyDepartMiddle> tbCompanyDepartMiddleList=null;

    public boolean add(TbCompanyDepartMiddle inTbCompanyDepartMiddle){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_CompanyDepartMiddle(CompanyId,DepartId) values(?,?)";
        pt = conn.prepareStatement(sql);
        System.out.println("==="+inTbCompanyDepartMiddle.getCompanyId()+"======="+inTbCompanyDepartMiddle.getDepartId());
        pt.setInt(1,inTbCompanyDepartMiddle.getCompanyId());
        pt.setInt(2,inTbCompanyDepartMiddle.getDepartId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public boolean del(int inCompanyDepartId){
        return UtilDAO.del("Tb_CompanyDepartMiddle", "CompanyDepartId", inCompanyDepartId);
    }
    public boolean edit(TbCompanyDepartMiddle inTbCompanyDepartMiddle){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_CompanyDepartMiddle set CompanyDepartId=?,CompanyId=?,DepartId=?,CreateTime=? where CompanyDepartId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbCompanyDepartMiddle.getCompanyDepartId());
        pt.setInt(2,inTbCompanyDepartMiddle.getCompanyId());
        pt.setInt(3,inTbCompanyDepartMiddle.getDepartId());
        pt.setString(4,inTbCompanyDepartMiddle.getCreateTime());
        pt.setInt(5,inTbCompanyDepartMiddle.getCompanyDepartId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public TbCompanyDepartMiddle getTbCompanyDepartMiddleByCompanyDepartId(int inCompanyDepartId) {
        tbCompanyDepartMiddle=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CompanyDepartMiddle where CompanyDepartId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inCompanyDepartId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbCompanyDepartMiddle == null)tbCompanyDepartMiddle = getTbCompanyDepartMiddle(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbCompanyDepartMiddle;
    }
    public List<TbCompanyDepartMiddle> getTbCompanyDepartMiddleAll() {
        tbCompanyDepartMiddleList=new ArrayList<TbCompanyDepartMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CompanyDepartMiddle";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCompanyDepartMiddleList.add(getTbCompanyDepartMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbCompanyDepartMiddleList;
    }
    public List<TbCompanyDepartMiddle> getTbCompanyDepartMiddleAll(VskPage vp) {
        tbCompanyDepartMiddleList=new ArrayList<TbCompanyDepartMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CompanyDepartMiddle";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_CompanyDepartMiddle", "CompanyDepartId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCompanyDepartMiddleList.add(getTbCompanyDepartMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbCompanyDepartMiddleList;
    }
    public TbCompanyDepartMiddle getTbCompanyDepartMiddle(ResultSet rs) throws Exception{
        TbCompanyDepartMiddle tbCompanyDepartMiddle = new TbCompanyDepartMiddle();
        tbCompanyDepartMiddle.setCompanyDepartId(rs.getInt("CompanyDepartId"));
        tbCompanyDepartMiddle.setCompanyId(rs.getInt("CompanyId"));
        tbCompanyDepartMiddle.setDepartId(rs.getInt("DepartId"));
        tbCompanyDepartMiddle.setCreateTime(rs.getString("CreateTime"));
        return tbCompanyDepartMiddle;
    }
    /**
     * 根据公司ID得到其下的所有部门
     *@author qingyu zhang
     *@function:
     * @param companyId
     * @return
     *2011-6-15
     *上午09:04:57
     *ProjectManagerT
     *com.companydepartmiddlemodule.dao
     *List<TbCompanyDepartMiddle>
     */
    public List<TbCompanyDepartMiddle> getCompanyDepartList(int companyId){
    	 try {
			conn = ConnectDB.getSqlServerConnection();
			sql="select * from Tb_CompanyDepartMiddle where CompanyId="+companyId;
			pt = conn.prepareStatement(sql);
			rs = pt.executeQuery();
			while(rs.next()){
				if(tbCompanyDepartMiddleList==null){
					tbCompanyDepartMiddleList = new ArrayList<TbCompanyDepartMiddle>();
				}
				tbCompanyDepartMiddleList.add(this.getTbCompanyDepartMiddle(rs));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
                        ConnectDB.closeSqlConnection();        
                    }
		return tbCompanyDepartMiddleList;
    }
    /**
     * 根据部门ID得到公司部门中间表记录
     *@author qingyu zhang
     *@function:
     * @param departId
     * @return
     *2011-6-16
     *上午10:15:09
     *ProjectManagerT
     *com.companydepartmiddlemodule.dao
     *List<TbCompanyDepartMiddle>
     */
    public List<TbCompanyDepartMiddle> getCompanyDepartListByDepartId(int departId){
    	try {
    		conn = ConnectDB.getSqlServerConnection();
    		sql="select * from Tb_CompanyDepartMiddle where DepartId="+departId;
    		pt = conn.prepareStatement(sql);
    		rs = pt.executeQuery();
    		while(rs.next()){
    			if(tbCompanyDepartMiddleList==null){
    				tbCompanyDepartMiddleList = new ArrayList<TbCompanyDepartMiddle>();
    			}
    			tbCompanyDepartMiddleList.add(this.getTbCompanyDepartMiddle(rs));
    		}
    	} catch (Exception e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}finally {
    		ConnectDB.closeSqlConnection();        
    	}
    	return tbCompanyDepartMiddleList;
    }
    
}
